//
//  LXDSingleCollectionViewCell.m
//  BLDMusics
//
//  Created by lxd on 16/3/23.
//  Copyright © 2016年 李晓丹. All rights reserved.
//

#import "LXDSingleCollectionViewCell.h"

@interface LXDSingleCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *modelArr;
@property (nonatomic, assign) NSInteger page;

@property (nonatomic, retain) NSMutableArray *songList;   /**< 代书义添加的 */

@end

@implementation LXDSingleCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createTabelView];
    }
    return self;
}

-(void)layoutSubviews {
    _tableView.frame = self.bounds;
}

- (void)createTabelView {
    _page = 1;
    self.modelArr = [NSMutableArray array];
    
    self.tableView = [[UITableView alloc] init];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.contentView addSubview:_tableView];
    _tableView.contentOffset = CGPointMake(0, 0);
    
    [_tableView registerClass:[BYJCarouselTableViewCell class] forCellReuseIdentifier:@"carouse"];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
}

// set 传值
- (void)setID:(NSNumber *)ID {
    _ID = ID;
    [self dataHandle];
    [self addFooter];
}


#pragma mark - 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (_modelArr.count) {
        return _modelArr.count + 1;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
        
        UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
        playButton.frame = CGRectMake(10, 20, 30, 30);
        [playButton setImage:[UIImage imageNamed:@"PlayPressed"] forState:UIControlStateNormal];
        [playButton addTarget:self action:@selector(playButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:playButton];
        
        UILabel *playLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 20, 100, 30)];
        playLabel.text= @"播放全部";
        [cell addSubview:playLabel];
        
        UIButton *manageButton = [UIButton buttonWithType:UIButtonTypeCustom];
        manageButton.frame = CGRectMake(SCREEN_WIDTH - 50, 20, 50, 50);
        [manageButton setImage:[UIImage imageNamed:@"audioeffect_equalizer_checked"] forState:UIControlStateNormal];
        [manageButton addTarget:self action:@selector(manageButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell addSubview:manageButton];
        
        return cell;
    }
    BYJCarouselTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"carouse"];
    BYJsongListModel *model= _modelArr[indexPath.row - 1];
    cell.model = model;
    cell.indexPath = indexPath.row;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    DSYMusicViewController *musicVC = [DSYMusicViewController sharedMusicPlayingVC];
    
    if (indexPath.row == 0) {
        musicVC.index = 0;
    } else {
        
        musicVC.index = indexPath.row - 1;
    }
    musicVC.isLocal = NO;
    
    
    DSYNAVViewController *nav = [DSYNAVViewController shareNavWithRootViewController:musicVC];
    
    
    [PlayViewController sharePlayViewBar].hidden = YES;
    
    [[self getController] presentViewController:nav animated:YES completion:nil];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}


// 获取当前navigation
- (UIViewController *)getController {
    
    for (UIView* next = [self superview];next; next = next.superview) {
        
        UIResponder* nextResponder = [next nextResponder];
        
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            
            return (UIViewController *)nextResponder;
            
        }
        
    }
    return nil;
}



// 播放全部的方法
- (void)playButtonAction:(UIButton *)button {
    
}
// 管理的方法
- (void)manageButtonAction:(UIButton *)button {
    
    BYJManageViewController *VC = [[BYJManageViewController alloc] init];
//    VC.dataSource = _model.songListArr;
    UINavigationController *nav = [self getViewController];
    [nav pushViewController:VC animated:YES];
    
}

// 获取当前navigation
- (UINavigationController*)getViewController {
    
    for (UIView* next = [self superview];next; next = next.superview) {
        
        UIResponder* nextResponder = [next nextResponder];
        
        if ([nextResponder isKindOfClass:[UINavigationController class]]) {
            
            return (UINavigationController *)nextResponder;
        }
        
    }
    return nil;
}

#pragma mark - 数据解析
- (void)dataHandle {
    
    if (self.page <= 1) {
        [[DSYDataManager shareDataManager].allDataArray removeAllObjects];
    }
    
    
    
    NSLog(@"%@", [NSString stringWithFormat:@"http://api.dongting.com/song/singer/%@/songs?page=%ld&size=50", _ID, _page]);
    
    [APPTools GETWithURL:[NSString stringWithFormat:@"http://api.dongting.com/song/singer/%@/songs?page=%ld&size=50", _ID, _page] par:nil success:^(id responseObject) {
        
        NSArray *tempArr = [BYJsongListModel modelHandleWithArray:responseObject[@"data"]];
        
        self.songList = [DSYSongModel modelHandleWithArray:responseObject[@"data"]];
        [[DSYDataManager shareDataManager].allDataArray addObjectsFromArray:self.songList];
        
        
        [self.modelArr addObjectsFromArray:tempArr];
        
        [_tableView reloadData];
        
        [_tableView.mj_footer endRefreshing];
        
        if (tempArr.count == 0) {
            [_tableView.mj_footer endRefreshingWithNoMoreData];
        }
        
    } filed:^(NSError *error) {
        
    }];
    
}

- (void)addFooter {
    // 上拉刷新-默认
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page ++;
        [self dataHandle];
    }];
    _tableView.mj_footer.automaticallyHidden = YES;
}


@end
